<?php

class merchant extends actionAbstract {


    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
        $this->partnerInfo = $this->userinfo=acl::checkLogin('PmsCookie','/index.php/partner/common/login');
        if($this->partnerInfo['parentid']!=0){
        	$this->partnerInfo['id'] = $this->partnerInfo['parentid'];
        }
    }
    //本酒店信息
    function hotelziduan($ziduan){
        $this->loadModel('hotel','category');
        $re=$this->hotel->categoryModel->fetchRow("select $ziduan from hotel_category where id=".(isset($_SESSION['hotel_id'])?$_SESSION['hotel_id']:0)." and partnerid=".$this->partnerInfo['id']);
    	if($re){
        	return $re[$ziduan];
        }else{
        	return 0;
        }
    }
    
    /**
     * 酒店管理
     */
    function category(){
    	$this->loadHelper('extend');
    	$leftpost="user";
    	$leftbot="category";
    	$this->loadModel('hotel','category');
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
    	$this->loadHelper('pager');
    	$page=isset($_GET['page'])?(int)($_GET['page']):1;
    	$size = 10;
    	$offset = ($page - 1) * $size;
    	$sql="select a.*,b.name as pName,c.name as cName,d.name as dName from hotel_category as a left join tc_province as b on b.osiiid=a.province";
    	$sql.=" left join tc_city as c on c.osiiid=a.city left join tc_county as d on d.osiiid=a.county WHERE a.partnerid=".$this->partnerInfo['id'];
    	$re = $this->hotel->categoryModel->fetchAll($sql);
    	
    	$count=count($re);
    	$number = ceil( $count / $size );
    	$extend = new pager();
    	$pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
    	$sql="select a.*,b.name as pName,c.name as cName,d.name as dName from hotel_category as a left join tc_province as b on b.osiiid=a.province";
    	$sql.=" left join tc_city as c on c.osiiid=a.city left join tc_county as d on d.osiiid=a.county WHERE a.partnerid=".$this->partnerInfo['id']." order by id asc limit $offset,$size";
    	$hotellist = $this->hotel->categoryModel->fetchAll($sql);
    	$province=$this->tc->provinceModel->fetchAll("select * from tc_province");
        
    	include $this->loadWidget('partnerTheme');
    }
    
    //添加、修改酒店
    function editcategory(){
    	$this->loadModel('hotel','category');
        $this->loadHelper('extend');
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
    	$this->loadHelper('js');
    	$js=new js();
    	if($_POST){
    		$hotelName=isset($_POST['hotelName'])?$_POST['hotelName']:'';
    		$province=isset($_POST['province'])?(int)$_POST['province']:0;
    		$city=isset($_POST['city'])?(int)$_POST['city']:0;
    		$county=isset($_POST['county'])?(int)$_POST['county']:0;
    		$address=isset($_POST['address'])?$_POST['address']:'';
    		
            if(empty($hotelName)){
                ajaxReturn ('','请填写酒店名称！', 0 );exit;
            }
            $sql="select id from hotel_category where partnerid=".$this->partnerInfo['isbusiness']." and hotelName='".$hotelName."'";
    	    $th_hotelName=$this->hotel->categoryModel->fetchRow($sql);
            if($th_hotelName){
                ajaxReturn ('','该酒店已存在你的酒店列表里！', 0 );exit;
            }
            
    		if(empty($province)){
    			ajaxReturn ('','请选择省！', 0 );exit;
    		}
    		if(empty($city)){
    			ajaxReturn ('','请选择市！', 0 );exit;
    		}
    		if(empty($county)){
    			ajaxReturn ('','请选择区！', 0 );exit;
    		}
    		if(empty($address)){
                ajaxReturn ('','请填写地址！', 0 );exit;
            }
            
    		$data['status']=1;
    		$data['created']=time();
			//添加酒店信息
			$arr['hotelName']=$hotelName;
			$arr['partnerid']=$this->partnerInfo['id'];
			$arr['province']=$province;
			$arr['city']=$city;
			$arr['county']=$county;
			$arr['address']=$address;
			$arr['status']=1;
			//echo "<pre>";print_r($arr);exit;
			$hotelarr=$this->hotel->categoryModel->insert($arr);
			/**
			 * 传输给OSII
			*/
			$provinceName=$this->tc->provinceModel->fetchRow("select name from tc_province where osiiid=$province");
			$cityName=$this->tc->cityModel->fetchRow("select name from tc_city where osiiid=$city");
			$countyName=$this->tc->countyModel->fetchRow("select name from tc_county where osiiid=$county");
	
			$this->loadApi('osiiHotel');
			$osiiHotel=new osiiHotel();
	
			$body=array();
			$body[0]['hotelCode']=$hotelarr;           $body[0]['hotelName']=$hotelName;                 $body[0]['hotelEnName']="";                         $body[0]['hotelDescription']="";
			$body[0]['star']=0;                        $body[0]['provinceCode']=$province;               $body[0]['provinceName']=$provinceName['name'];     $body[0]['cityCode']=$city;
			$body[0]['cityName']=$cityName['name'];    $body[0]['countyCode']=$county;                   $body[0]['countyName']=$countyName['name'];         $body[0]['brief']="";
			$body[0]['openYear']="2000-01-01";         $body[0]['roomQuantity']=0;                       $body[0]['telephone']="";                           $body[0]['diyBreakfast']=0;
			$body[0]['chineseBreakfast']=0;            $body[0]['westBreakfast']=0;                      $body[0]['fax']="";                                 $body[0]['address']=$address;
			//echo "<pre>";print_r($body);exit;
			$reosii=$osiiHotel->hotels($body);
			ajaxReturn ('refurbish','操作成功！', 1 );exit;
    		
    	}
    }
    
    //酒店状态
	public function status(){
        $this->loadModel('pms','account');
        $this->loadModel('hotel','category');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->hotel->categoryModel->fetchRow("select * from hotel_category where id=$id");
            if($result&&$result['status']==1){
                $data['status']=2;
            }elseif($result&&$result['status']==2){
                $data['status']=1;
            }
            $re=$this->hotel->categoryModel->update($data,"id=$id");
            if($re){
                
                /**
                 * 传送 OSSII
                 */
                $this->loadApi('osiiHotel');
                $osiiHotel=new osiiHotel();
                $bodys=array();
                $bodys['hotelCode']=$re['id'];          $bodys['hotelStatus']=$data['status'];
                $hotelStatus_osii=$osiiHotel->setHotelStatus($bodys);
                ajaxReturn ('back','修改成功！', 1 );exit;
            }else{
                ajaxReturn ('','修改失败！', 0 );exit;
            }
        }
    }
    
    /**
     * 锁定酒店
     */
    public function hotelLock(){
    	$_SESSION['hotel_id'] = $_GET['id'];
    	header("location:/index.php/partner/index/main");
    }
    
    //省市区选择
    function get_address(){
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $parentid=isset($_POST['parentid'])?(int)$_POST['parentid']:0;
        $type=isset($_POST['type'])?(int)($_POST['type']):0;
        if($type==1){
            $re=$this->tc->cityModel->fetchAll("select * from tc_city where province_id=$parentid");
        }elseif($type==2){
            $re=$this->tc->countyModel->fetchAll("select * from tc_county where city_id=$parentid");
        }
        $html="";
        $html.="<option value='0'>请选择</option>";
        if(!empty($re)){
            foreach($re as $k=>$v){
                $html.="<option value='".$v['osiiid']."'>".$v['name']."</option>";
            }
        }
        exit(json_encode(array('type'=>$type,'html'=>$html)));
    }
}